package util;

import java.awt.event.KeyEvent;

import javax.media.opengl.GL2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import templates.JOGLTemplate;

public class Camera<T extends JOGLTemplate> {

	private static Logger LOGGER = LoggerFactory.getLogger(Logger.class);

	T template;
	private double zoomDistance;

	public Camera(T template) {
		this.template = template;
	}

	public void moveCamera(int keyCode) {
		switch (keyCode) {
		case KeyEvent.VK_NUMPAD2:
			template.rotate(0, 1);
			break;
		case KeyEvent.VK_NUMPAD8:
			template.rotate(0, -1);
			break;
		case KeyEvent.VK_NUMPAD6:
			template.rotate(1, 0);
			break;
		case KeyEvent.VK_NUMPAD4:
			template.rotate(-1, 0);
			break;
		case KeyEvent.VK_NUMPAD5:
			resetCamera();
			break;
		case KeyEvent.VK_UP:
			LOGGER.info("Move Forward !");
			template.move(0, 0, template.getSpeed());
			break;
		case KeyEvent.VK_DOWN:
			LOGGER.info("Move Backward !");
			template.move(0, 0, -template.getSpeed());
			break;
		case KeyEvent.VK_PAGE_UP:
			LOGGER.info("Accelerate !");
			template.accelerate(1);
			break;
		case KeyEvent.VK_PAGE_DOWN:
			LOGGER.info("Decelerate !");
			template.accelerate(-1);
			break;
		}

	}

	public void zoom(double zoom) {
		if (zoom < 0)
			zoomIn(zoom);
		else
			zoomOut(zoom);
	}

	private void zoomIn(double zoom) {
		zoomDistance = zoomDistance + 1;
	}

	private void zoomOut(double zoom) {
		zoomDistance = zoomDistance - 1;
	}

	private void resetCamera() {
		template.resetCamera();
	}

	// Accessors
	public T getTemplate() {
		return template;
	}

	public double getZoomDistance() {
		return zoomDistance;
	}

	public void setZoomDistance(double zoomDistance) {
		this.zoomDistance = zoomDistance;
	}

	public void moveForward(GL2 gl) {
		gl.glTranslatef(0.0f, 0.0f, (float) getZoomDistance());
	}

}
